﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Baseframework.Model;
using Honestysoft.DictionaryHelper;

namespace Honestysoft.Financing.WebUI.Module.Contract
{
    public partial class ContractCategory_List : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                AspNetPager1.PageSize = Globals.PageSize;
                Bind();
            }
        }
        /// <summary>
        /// 设置弹出窗体大小
        /// 基类中已自动做控制。默认宽度800，高度600
        /// </summary>
        protected override void SetDialogSize()
        {
            ModalDialogSize.EditHeight = 350;
            ModalDialogSize.EditWidth = 400;
            ModalDialogSize.ViewHeight = 350;
            ModalDialogSize.ViewWidth = 400;
        }
        /// <summary>
        /// 根据权限页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
                btnAdd.Attributes.Add("onclick", "javascript:return openDialog2('" + Honestysoft.Baseframework.WebUI.Globals.Home + "Module/Contract/ContractCategory_Edit.aspx?FunctionID=" + FunctionID + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增合同类别');");
            }
        }

        private QueryParameters GetQueryParameters()
        {
            QueryParameters queryParameters = new QueryParameters();
            queryParameters.Add("CategoryID", "ContractCategory", QueryParameters.OperatorType.Equal);
            if (txtDictName.Text.Trim().Length > 0)
            {
                queryParameters.Add("DictName", txtDictName.Text, QueryParameters.OperatorType.Like);
            }
            return queryParameters;
        }
        private OrderParameters GetOrderParameters()
        {
            OrderParameters orderParameters = new OrderParameters();
            orderParameters.Add("SortCode");
            return orderParameters;
        }
        /// <summary>
        /// 绑定列表
        /// </summary>
        private void Bind()
        {
            int pageCount = 0, recordCount = 0;
            DataTable dtList = new BaseBusinessLogic<Sys_Dictionary>().GetTablePaging(GetQueryParameters(), GetOrderParameters(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dtList;
            rptList.DataBind();
        }
        /// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId)
        {
            string script = "";
            if (PageSecurity.Edit)
            {
                script += ScriptHelper.LinkButton.GetGridEditButton(Honestysoft.Baseframework.WebUI.Globals.Home + "Module/Contract/ContractCategory_Edit.aspx", "修改合同类别", ModalDialogSize.EditWidth, ModalDialogSize.EditWidth, dataId, FunctionID);
            }
            if (PageSecurity.Delete)
            {
                if (script.Length > 0) { script += "&nbsp;"; }
                script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
            }
            if (PageSecurity.View)
            {
                if (script.Length > 0) { script += "&nbsp;"; }
                script += ScriptHelper.LinkButton.GetGridViewButton(Honestysoft.Baseframework.WebUI.Globals.Home + "Module/Contract/ContractCategory_View.aspx", "查看合同类别", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, dataId, FunctionID);
            }
            return script;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<Sys_Dictionary> logic = new BaseBusinessLogic<Sys_Dictionary>();
                logic.DeleteByKey(arrId);
                ScriptHelper.Window.Alert("删除成功！");
                Honestysoft.DictionaryHelper.DictionaryInstance.RemoveDictionary();
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
        }
        /// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <param name="isValid">记录有效状态</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId, bool isValid)
        {
            string script = "";
            if (!isValid)
            {
                if (PageSecurity.Active)
                {
                    script += ScriptHelper.LinkButton.GetGridActiveButton(dataId, btnActive.ClientID);
                }
            }
            else
            {
                if (PageSecurity.Cancel)
                {
                    script += ScriptHelper.LinkButton.GetGridCancelButton(dataId, btnCancel.ClientID);
                }
            }
            string script2 = GetButtonScript(dataId);
            if (script2.Length > 0) { script += "&nbsp;" + script2; }
            return script;
        }

        protected void btnActive_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<Sys_Dictionary> logic = new BaseBusinessLogic<Sys_Dictionary>();
                logic.UpdateColumn("IsValid", true, arrId);
                ScriptHelper.Window.Alert("启用成功！");
                Honestysoft.DictionaryHelper.DictionaryInstance.RemoveDictionary();
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("启用失败！系统发生了未知错误！");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<Sys_Dictionary> logic = new BaseBusinessLogic<Sys_Dictionary>();
                logic.UpdateColumn("IsValid", false, arrId);
                ScriptHelper.Window.Alert("禁用成功！");
                Honestysoft.DictionaryHelper.DictionaryInstance.RemoveDictionary();
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("禁用失败！系统发生了未知错误！");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Bind();
        }
    }
}
